<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <!-- <el-table-column label="供应商编号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.supplierId`" :rules="formRules.supplierId" class="mb-0px!">
            <el-input v-model="row.supplierId" placeholder="请输入供应商编号" />
          </el-form-item>
        </template>
      </el-table-column> -->
      <el-table-column label="产品名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productName`" :rules="formRules.productName" class="mb-0px!">
            <el-input v-model="row.productName" placeholder="请输入产品名称" :disabled="true"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品编号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productId`" :rules="formRules.productId" class="mb-0px!">
            <el-input v-model="row.productId" placeholder="请输入产品编号" :disabled="true"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品单位" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productUnit`" :rules="formRules.productUnit" class="mb-0px!">
            <el-input v-model="row.productUnit" placeholder="请输入产品单位" :disabled="true"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品规格" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productStandards`" :rules="formRules.productStandards" class="mb-0px!">
            <el-input v-model="row.productStandards" placeholder="请输入产品规格" :disabled="true"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="产品单价" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.productPrice`" :rules="formRules.productPrice" class="mb-0px!">
            <el-input v-model="row.productPrice" placeholder="请输入产品单价" :disabled="true"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="采购数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.countActual`" :rules="formRules.countActual" class="mb-0px!">
            <el-input v-model="row.countActual" placeholder="请输入数量" :disabled="true"/>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="已分发数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.nums`" :rules="formRules.nums" class="mb-0px!">
            <el-input v-model="row.nums" placeholder="暂无"  :disabled="true" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="一级库库存数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.num`" :rules="formRules.num" class="mb-0px!">
            <el-input v-model="row.num" placeholder="暂无"  :disabled="true" />
          </el-form-item>
        </template>
      </el-table-column>
      

      <el-table-column label="数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.countss`" :rules="formRules.countss" class="mb-0px!">
            <el-select v-model="row.countss" placeholder="请选择数量">
              <el-option
                v-for="item in getOptions(row)"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <!-- <el-table-column label="总价" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.totalPrice`" :rules="formRules.totalPrice" class="mb-0px!">
            <el-input v-model="row.totalPrice" placeholder="请输入总价" />
          </el-form-item>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="备注" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" :rules="formRules.remark" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column> -->

      <!-- <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column> -->
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <!-- <el-button @click="handleAdd" round>+ 添加ERP 科室采购申请项</el-button> -->
  </el-row>
</template>

<script setup lang="ts">
import { DepPurchaseApi } from '@/api/erp/deppurchase'

const props = defineProps<{
  orderId: undefined // 科室采购订单编号（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  orderId: [{ required: true, message: '科室采购订单编号不能为空', trigger: 'blur' }],
  supplierId: [{ required: true, message: '供应商编号不能为空', trigger: 'blur' }],
  productId: [{ required: true, message: '产品编号不能为空', trigger: 'blur' }],
  productUnit: [{ required: true, message: '产品单位不能为空', trigger: 'blur' }],
  productStandards: [{ required: true, message: '产品规格不能为空', trigger: 'blur' }],
  productPrice: [{ required: true, message: '产品单价不能为空', trigger: 'blur' }],
  count: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
  totalPrice: [{ required: true, message: '总价不能为空', trigger: 'blur' }],
  productName: [{ required: true, message: '产品名称不能为空', trigger: 'blur' }]
  
})

const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.orderId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await DepPurchaseApi.getnewDepPurchaseItemListByOrderId(val)
      // console.log('111',formData.value)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    orderId: undefined,
    supplierId: undefined,
    productId: undefined,
    productUnit: undefined,
    productStandards: undefined,
    productPrice: undefined,
    count: undefined,
    totalPrice: undefined,
    remark: undefined,
    productName: undefined,
  }
  row.orderId = props.orderId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

/** 获取下拉选项 */
const getOptions = (row) => {
  const options = [];
  const maxCount = Math.min(row.num, row.countActual, row.countActual - row.nums);
  for (let i = 0; i < maxCount; i++) {
    options.push({ value: i + 1, label: i + 1 });
  }
  return options;
}

defineExpose({ validate, getData })
</script>
